package com.material.contract.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.material.common.utils.DateUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.contract.domain.ContractTemplate;
import com.material.contract.mapper.ContractTemplateMapper;
import com.material.contract.vo.ContractInfoListVo;
import com.material.contract.vo.ContractInfoVo;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.mapper.FactoryInfoMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.material.contract.mapper.ContractInfoMapper;
import com.material.contract.domain.ContractInfo;
import com.material.contract.service.IContractInfoService;

/**
 * 合同信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class ContractInfoServiceImpl implements IContractInfoService
{
    @Autowired
    private ContractInfoMapper contractInfoMapper;
    @Autowired
    private FactoryInfoMapper factoryInfoMapper;
    @Autowired
    private ContractTemplateMapper contractTemplateMapper;

    /**
     * 查询合同信息
     *
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public ContractInfo selectContractInfoById(Long id)
    {
        return contractInfoMapper.selectContractInfoById(id);
    }

    /**
     * 查询合同信息列表
     *
     * @param contractInfo 合同信息
     * @return 合同信息
     */
    @Override
    public List<ContractInfo> selectContractInfoList(ContractInfo contractInfo)
    {
        return contractInfoMapper.selectContractInfoList(contractInfo);
    }

    /**
     * 新增合同信息
     *
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Override
    public int insertContractInfo(ContractInfo contractInfo)
    {
        Long templateId = contractInfo.getTemplateId();
        ContractTemplate contractTemplate = contractTemplateMapper.selectContractTemplateById(templateId);
        contractInfo.setContent(contractTemplate.getContent());
        contractInfo.setCreateTime(DateUtils.getNowDate());
        return contractInfoMapper.insertContractInfo(contractInfo);
    }

    /**
     * 修改合同信息
     *
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Override
    public int updateContractInfo(ContractInfo contractInfo)
    {
        Long templateId = contractInfo.getTemplateId();
        ContractTemplate contractTemplate = contractTemplateMapper.selectContractTemplateById(templateId);
        contractInfo.setContent(contractTemplate.getContent());
        return contractInfoMapper.updateContractInfo(contractInfo);
    }

    /**
     * 批量删除合同信息
     *
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractInfoByIds(Long[] ids)
    {
        return contractInfoMapper.deleteContractInfoByIds(ids);
    }

    /**
     * 删除合同信息信息
     *
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractInfoById(Long id)
    {
        return contractInfoMapper.deleteContractInfoById(id);
    }

    @Override
    public List<ContractInfoVo> selectContractList(ContractInfo query) {
        List<ContractInfo> contractInfos = this.selectContractInfoList(query);
        if(CollectionUtils.isEmpty(contractInfos)){
            return new ArrayList<>();
        }
        List<Long> factoryIds = contractInfos.stream().map(ContractInfo::getFactoryId).collect(Collectors.toList());
        List<FactoryInfo> factoryInfos = factoryInfoMapper.selectFactoryInfoByIds(factoryIds);
        Map<Long, String> factoryMap = factoryInfos.stream().collect(Collectors.toMap(FactoryInfo::getId, f -> f.getName()));
        List<ContractInfoVo> contractInfoVos = BeanUtil.copyProperties(contractInfos, ContractInfoVo.class);
        contractInfoVos.forEach(c->{
            c.setFactoryName(factoryMap.get(c.getFactoryId()));
        });
        return contractInfoVos;
    }

    @Override
    public List<ContractInfoListVo> selectContractInfoPageList(ContractInfo contractInfo) {
        return contractInfoMapper.selectContractInfoPageList(contractInfo);
    }
}
